package com.logic.landseaserver.ws;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.service.IProperty;
import com.logic.landseaserver.ws.dto.PropertyDTO;
import com.logic.landseaserver.ws.request.PropertyReq;
import com.logic.landseaserver.ws.request.PropertyStatusHistoryReq;
import com.logic.landseaserver.ws.request.StatisticsByStatusReq;
import com.logic.landseaserver.ws.request.SuspendPropertyReq;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * Created by Quintina on 2017/4/25.
 */
@Path("/property")
@Api(value = "物业")
public class PropertyRest {

    @Autowired
    private IProperty propertyService;
    @Autowired
    HttpServletRequest httpServletRequest;
    
    private Logger logger = LoggerFactory.getLogger(PropertyRest.class);

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "物业提交", position = 1)
    public Response insert(@ApiParam(value = "param", required = true) PropertyReq propertyReq) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(propertyService.insert(propertyReq));
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("insert error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("insert error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @PUT
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "物业修改", position = 2)
    public Response update(@ApiParam(value = "param", required = true) PropertyReq propertyReq) {
        CommonResultMap map = new CommonResultMap();
        try {
            propertyService.update(propertyReq);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("update error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("update error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @DELETE
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "物业删除", position = 3)
    public Response delete(@ApiParam(value = "param", required = true) @QueryParam("ids") String ids) {
        CommonResultMap map = new CommonResultMap();
        try {
            propertyService.delete(ids);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("delete error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("delete error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/list")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "物业列表", position = 2)
    public Response list(@ApiParam(value = "param", required = true) QueryUtil queryUtil) {
        CommonResultMap map = new CommonResultMap();
        List<PropertyDTO> projectDTOList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            projectDTOList = propertyService.queryPage(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(projectDTOList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("list error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    @POST
    @Path("/statisticsByStatus")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "分类统计", position = 4)
    public Response statisticsByStatus(@ApiParam(value = "param", required = true) StatisticsByStatusReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(propertyService.statisticsByStatus(req));
        } catch (BusinessException e) {
            logger.error("statisticsByStatus error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("statisticsByStatus error.", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取选址详情", position = 4)
    public Response retrieveProjectById(
            @ApiParam(value = "param", required = true) @PathParam("id") Integer id) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(propertyService.get(id));
        } catch (BusinessException e) {
            logger.error("retrieveProjectById error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("retrieveProjectById error.", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/suspendProperty")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "中止物业", position = 2)
    public Response suspendProperty(@ApiParam(value = "param", required = true) SuspendPropertyReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            propertyService.suspendProperty(req);
            map.setResult(CommonResult.SUCCESS);
        } catch (BusinessException e) {
            logger.error("suspendProperty error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("suspendProperty error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/checkSubmit")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "审批通过", position = 2)
    public Response checkSubmit(@ApiParam(value = "param", required = true) PropertyStatusHistoryReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            propertyService.checkSubmit(req);
            map.setResult(CommonResult.SUCCESS);
        } catch (BusinessException e) {
            logger.error("checkSubmit error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("checkSubmit error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @PUT
    @Path("/continueProperty")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "再次启动", position = 2)
    public Response continueProperty(@ApiParam(value = "param", required = true) PropertyStatusHistoryReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            propertyService.contineProperty(req);
            map.setResult(CommonResult.SUCCESS);
        } catch (BusinessException e) {
            logger.error("continueProperty error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("continueProperty error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/signSubmit")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "立项", position = 2)
    public Response signSubmit(@ApiParam(value = "param", required = true) PropertyStatusHistoryReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            propertyService.signSubmit(req);
            map.setResult(CommonResult.SUCCESS);
        } catch (BusinessException e) {
            logger.error("signSubmit error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("signSubmit error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/decisionSubmit")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "投决通过", position = 2)
    public Response decisionSubmit(@ApiParam(value = "param", required = true) PropertyStatusHistoryReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            propertyService.decisionSubmit(req);
            map.setResult(CommonResult.SUCCESS);
        } catch (BusinessException e) {
            logger.error("decisionSubmit error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("decisionSubmit error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/reportProperty")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "上报物业", position = 2)
    public Response reportProperty(@ApiParam(value = "param", required = true) PropertyStatusHistoryReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            propertyService.reportProperty(req);
            map.setResult(CommonResult.SUCCESS);
        } catch (BusinessException e) {
            logger.error("reportProperty error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("reportProperty error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @GET
    @Path("/exportPropertyExcel/{ids}")
    @Produces("application/vnd.ms-excel; charset=UTF-8")
    @ApiOperation(value = "导出物业选址信息", position = 3)
    public void exportApplyDetailExcel(@Context HttpServletResponse response, @Context HttpServletRequest request,
                                       @ApiParam(value = "query util", required = true) @PathParam("ids") String ids) {
        propertyService.exportPropertyList(response, ids);
    }


    @GET
    @Path("/getHistory/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "物业历史", position = 1)
    public Response getHistory(
            @ApiParam(value = "param", required = true) @PathParam("id") Integer id) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(propertyService.queryPropertyHistory(id));
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (Exception e) {
            logger.error("getHistory error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

}
